import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;

public class BruteCollinearPoints {
    
    public BruteCollinearPoints(Point[] points)    // finds all line segments containing 4 points
    {
        this.a=new LinkedList<LineSegment>();
       int length=points.length;                   //反正要求的最差时间复杂度是n^4，也就先不想办法优化这个了，关键在于下一个算法呢。
       for(int i=0;i<length;i++)
       {
        Comparator<Point> com=points[i].slopeOrder();
        for(int j=1;j<length;j++)
        {
            for(int k=2;k<length;k++)
            {
                for(int l=3;l<length;l++)
                {
                   if(i==j||j==k||k==l||i==k||i==l||j==l)        //保证四个点都不相等
                   {
                    break;
                   }
                   if(com.compare(points[j],points[k])==0&&com.compare(points[k],points[l])==0&&com.compare(points[j], points[l])==0)  //找到一个线段
                   {
                    Point a[]=new Point[4];
                    a[0]=points[i];a[1]=points[j];a[2]=points[k];a[3]=points[l];
                    InsertionSort sort=new InsertionSort();
                    sort.MySort(a);
                    LineSegment newone=new LineSegment(a[0],a[3]);
                    boolean judge=this.a.contains(newone);
                    if(!judge)
                    {
                    this.a.addFirst(newone);
                    numberOfSegments++;
                    }
                     break;
                   }
                }
            }
        }
       }
    }
    private int numberOfSegments;
   public           int numberOfSegments()        // the number of line segments
   {
       return numberOfSegments;
   }
   private LinkedList<LineSegment> a;
   public LineSegment[] segments()                // the line segments
   {
     LineSegment[] segments=new LineSegment[numberOfSegments];
     int i=0;
     while(a.size()!=0)
     {
        segments[i]=a.removeFirst();
        i++;
     }
     return segments;
   }
}
